# *************************** 收集项目 Top根目录 和 子目录
macro(SetCurrentIncludeDirs DIRS)
    # macro和函数不同, 此处 DIRS 存储变量名称, 例如: ${DIR} -> indir
    set(${DIRS} ${CMAKE_CURRENT_SOURCE_DIR})

    # PATHS 是变量名
    file(GLOB_RECURSE PATHS LIST_DIRECTORIES true *)

    foreach(PATH ${PATHS})
        if(IS_DIRECTORY ${PATH})
            list(APPEND ${DIRS} ${PATH})
        endif()
    endforeach()

    # 此处需要两次解引用才能给出 value
    # ${${DIRS}}->${indir}-> "C:/dir1;C:/dir2;C:/dir2;"
    set(${DIRS} ${${DIRS}} CACHE INTERNAL "")
    include_directories(${${DIRS}})
endmacro()

# ***************************
# 如果存在子项目 CMakeLists.txt，则添加子项目; 并且设置变量 MOD_NAME 的值为 MOD_NAME
macro(AddSubmodule MOD_NAME)
    # set(${MOD_NAME} ${MOD_NAME})
    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${MOD_NAME}/CMakeLists.txt")
        message(STATUS "[ymy] exist: ${MOD_NAME}")
        add_subdirectory("${MOD_NAME}")
    endif()
endmacro()

macro(my_set_var_literal NAME)
    set(${NAME} ${NAME})
endmacro()

# *************************** 安装 SDK
macro(InstallSDK)
    file(REMOVE_RECURSE "${PATH_SDK}/include/${LIB_NAME}")
    install(FILES ${SDK_HEADERS} DESTINATION "${PATH_SDK}/include/${LIB_NAME}")

    if(MSVC)
        install(TARGETS ${LIB_NAME}
            RUNTIME DESTINATION "${PATH_SDK}/bin/$<CONFIG>"
            LIBRARY DESTINATION "${PATH_SDK}/bin/$<CONFIG>"
            ARCHIVE DESTINATION "${PATH_SDK}/lib/$<CONFIG>"
        )
    else()
        install(TARGETS ${LIB_NAME}
            RUNTIME DESTINATION "${PATH_SDK}/bin"
            LIBRARY DESTINATION "${PATH_SDK}/lib"
        )
    endif()
endmacro()

# *************************** 设置目录名称
macro(SetFolder VAR)
    string(REGEX REPLACE ".*/(.*)" "\\1" ${VAR} ${CMAKE_CURRENT_SOURCE_DIR})
endmacro()

# *************************** copy project dependent dlls to binary dir, windows
function(my_copy_prj_dlls target)
    if(WIN32)
        add_custom_command(TARGET "${target}" POST_BUILD COMMAND
            ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:${target}>
            $<TARGET_FILE_DIR:${target}>
            COMMAND_EXPAND_LISTS
        )
    endif()
endfunction()

# *************************** 安装脚本
# 在 cmake 的 build 目录下运行: cmake --build . --target install
# -- 如果用户没有显式指定, 默认安装到源代码目录的 install-demo 下面
function(my_install_script prj_name prj_headers)
    # 默认输出位置
    if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
        set_property(CACHE CMAKE_INSTALL_PREFIX PROPERTY VALUE "${CMAKE_SOURCE_DIR}/install-demo")
    endif()

    # 项目的 public header
    set_target_properties(${prj_name} PROPERTIES PUBLIC_HEADER "${prj_headers}")

    # --- Install the target and create export-set; 名称为 xxxTargets
    # Install target Output Artifacts and associated files:
    install(TARGETS ${prj_name}
        EXPORT ${prj_name}Targets
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        RUNTIME DESTINATION bin
        PUBLIC_HEADER DESTINATION include)

    # Generate the version file for the config file
    include(CMakePackageConfigHelpers)
    write_basic_package_version_file(
        ${CMAKE_CURRENT_BINARY_DIR}/${prj_name}ConfigVersion.cmake
        VERSION ${PACKAGE_VERSION}
        COMPATIBILITY SameMajorVersion)

    # --- Exporting Targets from the Build Tree
    # Install a CMake file exporting targets for dependent projects:
    install(EXPORT ${prj_name}Targets
        DESTINATION "lib/cmake/${prj_name}")

    # Create config file
    configure_package_config_file(
        ${CMAKE_SOURCE_DIR}/Config.cmake.in ${prj_name}Config.cmake
        INSTALL_DESTINATION "lib/cmake/${prj_name}")

    # Install config files
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${prj_name}Config.cmake
        ${CMAKE_CURRENT_BINARY_DIR}/${prj_name}ConfigVersion.cmake
        DESTINATION "lib/cmake/${prj_name}")
endfunction()

# ************************ 打印, 函数

# ====================== 打印提示符
macro(my_prt_info msg)
    message("\n***** ${msg}")
endmacro()

# ====================== 打印开始 separator
macro(my_seg_start NAME)
    message("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxx startof [${NAME}]")
endmacro()

macro(my_seg_end NAME)
    message("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx endof [${NAME}]\n")
endmacro()

# ====================== 打印间隔 separator
macro(my_subsep)
    message("")
endmacro()

macro(my_test_cmake_current_list_file)
    message(STATUS "[ymy] CMAKE_CURRENT_LIST_FILE => ${CMAKE_CURRENT_LIST_FILE}")
endmacro()

# ====================== platform detect
set(MY_IS_APPLE_CLANG FALSE)

# ----- apple clang
message(STATUS "[ymy] $APPLE => ${APPLE}, $CMAKE_CXX_COMPILER_ID => ${CMAKE_C_COMPILER_ID}")
if(APPLE OR CMAKE_CXX_COMPILER_ID STREQUAL AppleClang)
    set(MY_IS_APPLE_CLANG TRUE)
    add_compile_definitions(_MY_NO_DYLIB_EXPORTS)
    message(STATUS "[ymy] platform: Apple-clang")
endif()

# ----- linux gcc
set(MY_IS_LINUX_GCC FALSE)

if(LINUX AND CMAKE_CXX_COMPILER_ID STREQUAL GNU)
    set(MY_IS_LINUX_GCC TRUE)
    add_compile_definitions(_MY_NO_DYLIB_EXPORTS)
    message(STATUS "[ymy] platform: linux-gcc")
endif()

# ----- windows MSVC
set(MY_IS_WIN_MSVC FALSE)

if(MSVC)
    set(MY_IS_WIN_MSVC TRUE)
    message(STATUS "[ymy] platform: windows-MSVC")
endif()